#include <QtGui>
#include <QIcon>

#include "newsviewer.hpp"
#include "browserwindow.hpp"

NewsViewer::NewsViewer(int newsnum,string newscontent[],string newslink[],QWidget * parent):QDialog(parent)
{
    setupUi(this);
    QIcon icon(":/rss.png");
    setWindowIcon(icon);
    currentnewsnum=0;
    this->newsnum=newsnum;
    for(int i=0;i<newsnum;i++)
    {
        this->newscontent[i]=QString::fromStdString(newscontent[i]);
        this->newslink[i]=QString::fromStdString(newslink[i]);
    }


    newsLabel->setText(this->newscontent[currentnewsnum]);

    connect(nextButton,SIGNAL(clicked()),this,SLOT(nextNews()));
    connect(previousButton,SIGNAL(clicked()),this,SLOT(previousNews()));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
    connect(openBrowserButton,SIGNAL(clicked()),this,SLOT(openBrowser()));
}

void NewsViewer::nextNews()
{
    if(currentnewsnum==newsnum-1)
        currentnewsnum=0;
    else
        currentnewsnum++;

    newsLabel->setText(this->newscontent[currentnewsnum]);
}

void NewsViewer::previousNews()
{
    if(currentnewsnum==0)
        currentnewsnum=newsnum-1;
    else
        currentnewsnum--;

    newsLabel->setText(this->newscontent[currentnewsnum]);
}

void NewsViewer::openBrowser()
{
    BrowserWindow *browserwindow = new BrowserWindow(newslink[currentnewsnum]);
    browserwindow->show();
}
